package com.xea.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.xea.model.Channel;
import com.xea.service.ChannelService;
import com.xea.service.ProductService;
import com.xea.service.ServiceFactory;

public class ChannelAction extends DispatcherServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8972389717478445191L;
	
	/**
	 * 查询所有类别
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void search(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ChannelService channelDao = ServiceFactory.getChannelService();
		request.setAttribute("datas", channelDao.search());
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("inner/admin/channel/left.jsp");
		dispatcher.forward(request, response);	
	}
	
	/**
	 * 查看类别详情
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException 
	 */
	public void detail(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		ChannelService channelDao = ServiceFactory.getChannelService();
		String id=request.getParameter("id");
		Channel chan=channelDao.findById(Integer.valueOf(id));
		request.setAttribute("channel", chan);
		if(chan.getParentId()!=0){
			request.setAttribute("chan", channelDao.findById(Integer.valueOf(chan.getParentId())));
		}
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("inner/admin/channel/detail.jsp");
		dispatcher.forward(request, response);
	}
	
	/**
	 * 新增类别
	 * @param request
	 * @param response
	 * @throws Exception 
	 */
	public void add(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ChannelService channelDao = ServiceFactory.getChannelService();
		String name=request.getParameter("name");
		String parentId=request.getParameter("parentId");
		String remark=request.getParameter("remark");
		String type=request.getParameter("type");
		response.setCharacterEncoding("utf-8");
		JSONObject json=new JSONObject();
		if(channelDao.find(Integer.valueOf(parentId), name)!=null){
			PrintWriter out = response.getWriter();
			json.put("msg", "该名称已在该类别存在，请重新输入！");
			out.print(json.toString());
			out.close();
			return;
		}
		Channel channel=new Channel(Integer.valueOf(parentId), name, remark,Integer.valueOf(type));
		channelDao.add(channel);
	}
	
	/**
	 * 删除类别
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void del(HttpServletRequest request,HttpServletResponse response) throws IOException{
		ChannelService channelDao = ServiceFactory.getChannelService();
		ProductService productDao = ServiceFactory.getProductService();
		String parentId=request.getParameter("parentId");
		String id=request.getParameter("id");
		response.setCharacterEncoding("utf-8");
		JSONObject json=new JSONObject();
		if(channelDao.find(Integer.valueOf(parentId))!=null){
			PrintWriter out = response.getWriter();
			json.put("msg", "删除失败，该类别还有子类别！");
			out.print(json.toString());
			out.close();
			return;
		}
		if(productDao.find(Integer.valueOf(id))!=null){
			PrintWriter out = response.getWriter();
			json.put("msg", "删除失败，该类别还有产品！");
			out.print(json.toString());
			out.close();
			return;
		}
		channelDao.del(id);
	}
	
	/**
	 * 查询类别详情
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException 
	 */
	public void findById(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		ChannelService channelDao = ServiceFactory.getChannelService();
		String id=request.getParameter("id");
		request.setAttribute("channel", channelDao.findById(Integer.valueOf(id)));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher(request.getParameter("_url"));
		dispatcher.forward(request, response);
	}
	
	/**
	 * 修改类别
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void update(HttpServletRequest request,HttpServletResponse response) throws IOException{
		ChannelService channelDao = ServiceFactory.getChannelService();
		String name=request.getParameter("name");
		String id=request.getParameter("id");
		String remark=request.getParameter("remark");
		String parentId=request.getParameter("parentId");
		response.setCharacterEncoding("utf-8");
		JSONObject json=new JSONObject();
		if(channelDao.find(name,Integer.valueOf(id),Integer.valueOf(parentId))!=null){
			PrintWriter out = response.getWriter();
			json.put("msg", "该名称已在该类别存在，请重新输入！");
			out.print(json.toString());
			out.close();
			return;
		}
		Channel channel=channelDao.findById(Integer.valueOf(id));
		channel.setName(name);
		channel.setRemark(remark);
		channelDao.update(channel);
	}
}
